package io.ouka.rule.engine;

import io.ouka.rule.domain.EngineResult;
import io.ouka.rule.domain.RootTreeNode;
import io.ouka.rule.domain.RuleTree;
import io.ouka.rule.domain.TreeNode;
import io.ouka.rule.logic.LogicFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Description:
 * User: Ouka
 * Date: 2020-06-08
 * Time: 21:18
 */
public abstract class BaseEngine extends EngineConfig implements IEngine {
    private Logger log = LoggerFactory.getLogger(BaseEngine.class);
    @Override
    public abstract EngineResult process(Long treeId, RuleTree treeRich, Map<String, String> decisionMatter);

    protected TreeNode engineDecisionMaker(RuleTree treeRich, Long treeId, Map<String, String> keys) {
        RootTreeNode rootTreeNode = treeRich.getRootTreeNode();
        Map<Long, TreeNode> treeNodeMap = treeRich.getTreeNodeMap();
        Long treeRootNodeId = rootTreeNode.getTreeRootNodeId();
        TreeNode treeNode = treeNodeMap.get(treeRootNodeId);
        while (treeNode.getNodeType().equals(1)) {
            String ruleKey = treeNode.getRuleKey();
            LogicFilter logicFilter = logicFilterMap.get(ruleKey);
            String value = logicFilter.getValue(treeId, keys);
            Long aLong = logicFilter.doFilter(value, treeNode.getTreeNodeLinkList());
            treeNode = treeNodeMap.get(aLong);
            log.info("决策树引擎=>[{}] treeId：[{}] treeNode：[{}] ruleKey：[{}] matterValue：[{}]", rootTreeNode.getName(), treeId, treeNode.getTreeNodeId(), ruleKey, value);
        }

        return treeNode;
    }
}
